<template>
  <div class="app-container">

    <div>
      <div>
        <span class="contentTitle">{{ groupDetail.displayName }}</span>
        <span class="contentSecTitle">分群用户明细</span>
      </div>
    </div>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-finished"
          size="small"
          v-hasPermi="['base:group:add']"
        >保存用户分群
        </el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-download"
          size="small"
          @click="handleExport"
          v-hasPermi="['base:group:add']"
        >下载
        </el-button>
      </el-col>
      <!-- <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="data"></right-toolbar> -->
      <right-toolbar  @queryTable="getList" ></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="groupList">
      <el-table-column
        v-for="(item, index) in data"
        :sortable="item.sortable"
        :key="index.key"
        :prop="item.key"
        v-if="item.visible"
        :label="item.label"
      >
        <template slot-scope="scope">
          <span v-if="item.key=='distinct_id'">
             <router-link :to="'/group/userPortrait?id=' + scope.row.distinct_id" class="linkId">
              {{ scope.row.distinct_id }}
             </router-link>
          </span>
          <label v-else>{{ scope.row[item.key] }}</label>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center"
                       class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <router-link :to="'/group/userPortrait?id=' + scope.row.distinct_id" class="linkId">
            用户画像
          </router-link>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {
  listGroup,
  getGroup,
  delGroup,
  addGroup,
  updateGroup,
  exportGroup,
  listGroupUserDetail,
  exportUserDetail
} from "@/api/base/group";
import {getTag} from "@/api/tags/tag";
import {getUserAttributeList} from "@/api/config/userattribute";
import {listPropertyByType} from "@/api/config/property";
import {eventTypeOptions} from "@/api/config/item";
import {exportType} from "@/api/overview/type";

export default {
  name: "userDetail",
  components: {},
  data() {

    return {
      // 遮罩层
      loading: true,
      exportLoading: false,
      currentGroupId:null,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      showSearch:false,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      open: false,
      form: {},
      // 弹出层标题
      title: "列目显示",
      groupList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id:undefined,
        name: undefined,
        updateType: undefined,
        status: undefined,
        onlyMe: '',
        lastCalcStatus: undefined,
        buildType: undefined,
      },
      transTitles: ['选择用户属性', '用户属性'],
      groupDetail: {},
      data: [],
      value: []
    };
  },
  created() {
    const groupId = this.$route.query && this.$route.query.id;
    const detailType = this.$route.query && this.$route.query.detailType;
    this.currentGroupId=groupId;
    console.log('id=='+groupId);
    console.log('detailType=='+detailType);
    this.getUserAttributeList();
    if (detailType == 'TAG'){
      this.getTagDetail(groupId);
    } else{
      this.getGroupDetail(groupId);
    }

  },
  methods: {
    filterMethod(query, item) {
      return item.label.indexOf(query) > -1;
    },
    getGroupDetail(id) {
      getGroup(id).then(response => {
        this.groupDetail = response.data;
        this.queryParams.id=this.groupDetail.id;
        this.queryParams.name=this.groupDetail.name;
        this.getList();
      });
    },
    getTagDetail(id) {
      getTag(id).then(response => {
        this.groupDetail = response.data;
        this.queryParams.id=this.groupDetail.id;
        this.queryParams.name=this.groupDetail.name;
        this.getList();
      });
    },

    getUserAttributeList() {
      var param = {status: 'Y'};
      getUserAttributeList(param).then(response => {
        this.userAttributeOptions = response.data;
        this.userAttributeOptions.forEach((attr, index) => {
          // console.log("attributeName=="+attr.attributeName);
          this.data.push({
            label: attr.attributeRemark,
            key: attr.attributeName,
            sortable: index < 6 ? true : false,
            visible: true
          });
        });
      });
    },
    /** 查询用户分群列表 */
    getList() {
      this.loading = true;
      listGroupUserDetail(this.queryParams).then(response => {
        this.groupList = response.rows;
        // console.log("groupList=="+this.groupList);
        this.total = response.total;
        this.loading = false;
      });
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        displayName: undefined,
        remark: undefined,
        updateType: undefined,
        taskType: undefined,
        userAttributes: [],
        userActions: [],
        userActionOrders: []
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {

      this.handleQuery();
    },
    handleExport() {
      this.$confirm('是否确认导出所有用户记录吗?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.exportLoading = true;
        return exportUserDetail(this.currentGroupId);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    }

  }
};
</script>
<style rel="stylesheet/scss" lang="scss">
.linkId {
  color: #559ff0;
  text-decoration: none;
  background-color: transparent;
  outline: none;
  cursor: pointer;
  transition: color .3s;
}

</style>
